<template>
  <div style="width: 600px;background: #fff;">
    <el-breadcrumb separator-class="el-icon-arrow-right">

      <el-breadcrumb-item :to="{ path: '/sys-admin' }"><i class="el-icon-s-promotion"></i>后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加商品</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-form :model="ruleForm" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>

      <el-form-item label="分类" prop="category">
        <el-select v-model="ruleForm.category" placeholder="请选择">
          <el-option
              v-for="item in categoryList"
              :key="item.id"
              :label="item.category"
              :value="item.category">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="分类" prop="category">
        <el-select v-model="ruleForm.class1" placeholder="请选择">
          <el-option
              v-for="item in classList"
              :key="item.id"
              :label="item.class1"
              :value="item.class1">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="简介" prop="intro">
        <el-input v-model="ruleForm.intro" placeholder="暂无"></el-input>
      </el-form-item>

      <el-form-item label="价格" prop="price">
        <el-input v-model="ruleForm.price" placeholder="暂无"></el-input>
      </el-form-item>

      <el-form-item label="原价" prop="oldPrice">
        <el-input v-model="ruleForm.oldPrice" placeholder="暂无"></el-input>
      </el-form-item>

      <el-form-item label="商品图片">
        <!--limit控制图片数量-->
        <el-upload
            name="pic"
            action="http://localhost:8090/productupload"
            list-type="picture-card"
            :on-success="handleSuccess"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove">
          <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="ruleForm.url" alt="">
        </el-dialog>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  data() {
    return {
      dialogVisible: false,
      categoryList:[],
      classList:[],
      ruleForm: {
        name: '',
        category: '',
        class1: '',
        intro: '',
        price: '',
        oldPrice: '',
        url: ''
      }
    };
  },
  methods: {
    loadClassList() {
      let url = 'http://localhost:8090/class';
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        this.classList = responseBody.data;
      });
    },
    loadCategoryList() {
      let url = 'http://localhost:8090/category';
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        this.categoryList = responseBody.data;
      });
    },
    handleSuccess(response,file,fileList){
      this.ruleForm.url = response;
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.ruleForm.url = file.url;
      this.dialogVisible = true;
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:8090/product/add-new';
          console.log('url = ' + url);
          this.ruleForm.url = "http://localhost:8090/product" + this.ruleForm.url;
          let formData = this.qs.stringify(this.ruleForm);
          console.log('formData：' + formData);
          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .post(url, formData).then((response)=> {
            let responseBody = response.data;
            if (responseBody.state == 20000) {
              this.$message({
                message: '添加商品成功！',
                type: 'success'
              });
              this.resetForm(formName);
            } else {
              this.$message.error(responseBody.message);
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  mounted() {
    this.loadCategoryList();
    this.loadClassList();
  }
}
</script>

<style>
div{
  text-align: left;
}
</style>